package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.Statistics_customerDto;
import edu.suda.xsap.service.Statistics_customerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Year;
import java.util.List;

@RestController
@RequestMapping("/statistics_customer")
@Slf4j
public class Statistics_customerController {

    @Autowired
    private Statistics_customerService statistics_customerService;

    @GetMapping("/year")
    public Result year(@RequestParam("year1")Year year1, @RequestParam("year2")Year year2) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<Statistics_customerDto> statistics_customerDtos = statistics_customerService.findYEAR(year1, year2);
            //
            return Result.success(statistics_customerDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @GetMapping("/month")
    public Result month(@RequestParam("year")Year year) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<Statistics_customerDto> statistics_customerDtos = statistics_customerService.findMONTH(year);
            //
            return Result.success(statistics_customerDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @GetMapping("/quarter")
    public Result quarter(@RequestParam("year")Year year) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<Statistics_customerDto> statistics_customerDtos = statistics_customerService.findQUARTER(year);
            //
            return Result.success(statistics_customerDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
}
